const Controller = require('egg').Controller;

const MockData = require('../../mock')

const fs = require('fs')

const JsonData = require('./data.json')

class HomeController extends Controller {
    //往数据库添加数据
    async mock() {
        const { ctx, app } = this

        // fs.writeFileSync(`${__dirname}/data.json`, JSON.stringify(MockData), () => { })

        await app.mysql.insert('热点', (JsonData))

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //获取热点数据
    async getHotList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 热点')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取话题数据
    async getTopicList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 话题')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取生活数据
    async getLiveList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 生活')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取睡眠数据
    async getSleepList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 睡眠')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取男性数据
    async getManList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 男性')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //热点搜索
    async getHotSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 热点 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //话题搜索
    async getTopicSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 话题 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //生活搜索
    async getLiveSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 生活 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //睡眠搜索
    async getSleepSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 睡眠 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //男性搜索
    async getManSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 男性 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取商城数据
    async getShopList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 商城')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //商城详情页
    async getShopDetail() {
        const { ctx, app } = this

        const { id } = ctx.request.body

        const data = await app.mysql.get('商城', { id })

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取商品列表
    async getDepartment() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 商品列表')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //加入购物车
    async getCarShop() {
        const { ctx, app } = this

        const { img, title, info, price, num, id } = ctx.request.body

        const data = await app.mysql.get('商城', { id })

        if (data) {
            await app.mysql.insert('购物车', { img, title, info, price, num })

            ctx.body = {
                code: 0,
                msg: '请求成功'
            }
        } else {
            ctx.body = {
                code: 1,
                msg: '请求失败'
            }
        }
    }

    //获取购物车数据
    async getCarList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 购物车')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //删除购物车
    async deleteCarList() {
        const { ctx, app } = this

        const { id } = ctx.request.body

        await app.mysql.delete('购物车', { id })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //商品列表加入购物车
    async getCarDepart() {
        const { ctx, app } = this

        const { img, title, info, price, num, id } = ctx.request.body

        const data = await app.mysql.get('商品列表', { id })

        if (data) {
            await app.mysql.insert('购物车', { img, title, info, price, num })

            ctx.body = {
                code: 0,
                msg: '请求成功'
            }
        } else {
            ctx.body = {
                code: 1,
                msg: '请求失败'
            }
        }
    }

    //商品列表搜索
    async getDepartSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 商品列表 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //商城搜索
    async getShopSearch() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 商城 where title like '%${value}%'`)

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }
}

module.exports = HomeController;